<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.model' );

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JAJobBoardModelJAMail extends JModel
{
	function getEmail ()
  	{
  		$cid = JRequest::getVar('cid');
  		$db = JFactory::getDBO();
  		$query = "SELECT email FROM #__users WHERE id IN ($cid)";
  		$db->setQuery($query);
  		$result = $db->loadObjectList();
  		$temp = array();
  		for ($i=0;$i<count($result);$i++)
  			$temp[] = $result[$i]->email;
  		$email = implode(',',$temp);
  		return $email;
  	}
  	function getData($task='send')
  	{
  		$temp = JRequest::getVar('emails');
  		
  		
  		$data = array();
  		$data['email_from'] = JRequest::getVar('email_from');
  		$data['subject'] = JRequest::getVar('subject');
  		if ($task!='send')
  		{
  			$temp = explode(',',$temp);
  			$data['emails'] = $temp[0];
  		}
  		else 
  			$data['emails'] = $temp;
  		$data['content'] = JRequest::getVar('content');
  		
  		return $data;
  	}
  	
  	function prepare_data($user_id,$mail_content)
  	{
  		
  		global $jbconfig,$mainframe;
  		$admin_user = JFactory::getUser();
  		$user = JFactory::getUser($user_id);
  		
  		$variable_email = JobBoardConstant::get_Variable_Email();
  		$update_data = array();
  		for ($i=0;$i<count($variable_email);$i++)
  		{
  			switch ($variable_email[$i]->value)
  			{
  				case '[USER_NAME]':
  					$update_data[$variable_email[$i]->value] = $user->name;
  					break;
  				case '[USER_EMAIL]':
  					$update_data[$variable_email[$i]->value] = $user->email;
  					break;
  				case '[ADMIN_NAME]':
  					$update_data[$variable_email[$i]->value] = $admin_user->name;
  					break;
  				case '[ADMIN_EMAIL]':
  					$update_data[$variable_email[$i]->value] = $admin_user->email;
  					break;
  				case '[CONTACT_EMAIL]':
  					$update_data[$variable_email[$i]->value] = $jbconfig['emails']->get('fromemail');;
  					break;
  				case '[SITE_URL]':
  					$update_data[$variable_email[$i]->value] = $mainframe->getCfg('live_site');
  					break;
  				case '[SITE_NAME]':
  					$update_data[$variable_email[$i]->value] = $jbconfig['emails']->get('sitname');
  					break;
  			}
  		}
  		return $update_data;
  		
  	}
  	
  	function updateMailContent($user_id,$mail_content)
  	{
  		$update_data = $this->prepare_data($user_id,$mail_content);
  		
  		if (is_array($update_data))
	  		foreach ($update_data as $var => $value)
	  		{
	  			
	  			$mail_content = str_replace($var,$value,$mail_content);
	  			
	  		}$temp = $mail_content;
	  		
	  	return $mail_content;
  	}
}